package com.roy.galaxy.algorithm;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019-10-10 09:50
 */
public class Power {

    public double Power1(double base, int exponent) {

        //指数为0时，结果为1
        if(exponent == 0)
            return 1;
        //底数为0时，结果为0
        if(base == 0)
            return 0;
        //指数为1时，结果为base
        if(exponent == 1)
            return base;

        int positiveExponent = Math.abs(exponent);
        double baseCopy = base;

        for(int i = 0; i < positiveExponent-1 ; i++){

            base = baseCopy*base;
        }
        return exponent < 0 ? 1 / base : base;
    }

    public static void main(String[] args) {
        System.out.println(new Power().Power1(2.0,-2));
    }
}
